package com.github.hongkaiwen.rxjava.play.problem1;

import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class ParallelSolve extends Base {


    public static void main(String... args) throws InterruptedException {
        ParallelSolve parallelSolve = new ParallelSolve();

        Flowable.just("a", "b", "c").parallel().runOn(Schedulers.io())
            .map(t -> dispatch(t, parallelSolve)).reduce((i, j) -> i + j).subscribe(parallelSolve::result);
        TimeUnit.SECONDS.sleep(6);
    }

    private static int dispatch(String target, ParallelSolve parallelSolve){
        if("a".equals(target)){
            return parallelSolve.a();
        } else if("b".equals(target)){
            return parallelSolve.b();
        } else {
            return parallelSolve.c();
        }
    }

}
